package org.colcon.web.model;

import java.util.HashMap;
import java.util.Map;

import org.colcon.web.model.framework.IBallot;
import org.colcon.web.model.framework.IVote;
import org.colcon.web.server.CLI;

public class Vote extends Container implements IVote
{
	//TODO: add voter whitelist
	
	public String getType(){return "vote";}
	
	private static final long serialVersionUID = 5206645418694019354L;
	
	public static final String

		// containers
		BALLOTS = "ballots";
	
	protected static final String
	
		// arguments
		TEXT = "text",
		COMMAND = "command",
		
		// vote result values
		INVALID_VOTE = "invalid_vote";
	
	public Vote()
	{
		this(new HashMap<String, String>());
	}
	
	public Vote(Map<String, String> args)
	{
		this(
				args.get(PARENT), 
				args.get(ID),
				args.get(TEXT), 
				args.get(COMMAND), 
				args.get(CHILDREN)
		);
	}
	
	public Vote(String parent, String id, String text, String command, String children)
	{
		super(parent, id, children);
		
		if(command == null)
			command = "null";
		
		setArg(PARENT, parent);
		setArg(COMMAND, command);
		
		if(text == null)
			text = super.getText();
		
		setArg(TEXT, text);
	}
	
	public String ballots()
	{
		return getPath() + '/' + BALLOTS;
	}
	
	public String getGroup()
	{
		return Group.pathToGroup(getParentPath());
	}
	
	public String getCommand()
	{
		return getArg(COMMAND);
	}
	
	public String getText()
	{
		return getArg(TEXT);
	}
	
	public String getValue(IBallot[] ballots)
	{
		return INVALID_VOTE;
	}
	
	public String getResult(IBallot[] ballots)
	{
		if(getCommand() == null || getValue(ballots) == null)
			return CLI.NULL;
		
		return getCommand().replaceAll("\\[" + getId() + "\\]", getValue(ballots));
	}
}
